package DataStructure.list;

//双向链表
public class DLinkList<E> implements MyList<E>{
    private Node head;//头节点
    private int size;

    //链表节点
    private class Node{
        private E e;
        private Node pre;
        private Node next;
        public Node(E e, Node pre,Node next){
            this.e=e;
            this.pre=pre;
            this.next=next;
        }
    }

    public DLinkList() {
        head=new Node(null,null,null);
        size=0;
    }

    //找到元素的前一个节点
    private Node getPre(E element){
        Node cur=head;
        while(cur.next!=head){
            if(cur.next.e.equals(element)){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    //找到索引的前一个节点
    private Node getPre(int index){
        if(index<0||index>size)
            return null;
        Node cur=head;
        int cnt=0;
        while (cnt != index) {
            cur = cur.next;
            cnt++;
        }
        return cur;
    }
    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element)!=-1;
    }

    @Override
    public boolean add(E element) {
        add(size,element);
        return true;
    }

    @Override
    public void add(int index, E element) {
        Node tmp=getPre(index);
        if(tmp==null)
            return;
        Node n=new Node(element,tmp,tmp.next);
        tmp.next= n;
        if(n.next!=null)
            n.next.pre=n;
        size++;
    }

    @Override
    public boolean remove(E element) {
        Node tmp=getPre(element);
        if(tmp==null||tmp.next==null)
            return false;
        if(tmp.next.next!=null)
            tmp.next.next.pre=tmp;
        tmp.next=tmp.next.next;
        size--;
        return true;
    }

    @Override
    public E remove(int index) {
        Node tmp=getPre(index);
        if(tmp==null||tmp.next==null)
            return null;
        Node res=tmp.next;
        if(res.next!=null)
            res.next.pre=tmp;
        tmp.next=res.next;
        size--;
        return res.e;
    }

    @Override
    public E get(int index) {
        Node tmp=getPre(index);
        if(tmp==null||tmp.next==null)
            return null;
        return tmp.next.e;
    }

    @Override
    public int indexOf(E element) {
        Node cur=head.next;
        int cnt=0;
        while(cur!=head){
            if(cur.e.equals(element))
                return cnt;
            cur=cur.next;
            cnt++;
        }
        return -1;
    }

    @Override
    public E set(int index, E element) {
        Node tmp=getPre(index);
        if(tmp==null||tmp.next==null)
            return null;
        E res=tmp.next.e;
        tmp.next.e=element;
        return res;
    }

    @Override
    public void clear() {
        head=new Node(null,null,null);
        size=0;
    }

    @Override
    public String toString() {
        StringBuilder sb=new StringBuilder("[");
        if(!isEmpty()){
            Node cur=head.next;
            while(cur!=null){
                sb.append(cur.e).append(",");
                cur=cur.next;
            }
            sb.deleteCharAt(sb.length()-1);
        }
        sb.append("]");
        return "DLinkList{" +
                "head=" + sb +
                ", size=" + size +
                '}';
    }
}
